/*
 * SearchProjectsController.java
 *
 * Project: TECHWATCH - TESTING TECHNOLOGIES
 *
 * Copyright 2008 by ELCA Informatique SA
 * Av. de la Harpe 22-24, 1000 Lausanne 13
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of ELCA Informatique SA. ("Confidential Information"). You
 * shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license
 * agreement you entered into with ELCA.
 */

package ch.elca.training.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.HttpSessionRequiredException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import ch.elca.training.dom.Project;
import ch.elca.training.dom.ProjectQuery;
import ch.elca.training.model.UrlConstants;
import ch.elca.training.service.ProjectService;

/**
 * This controller manages all action on the search projects screen.
 * 
 * <script type="text/javascript">printFileStatus ("$URL:
 * https://cvs.elca.ch/subversion
 * /cxnet-portal/trunk/etc/eclipse/preferences_2008_03_03.epf $", "$Revision:
 * 334 $", "$Date: 2008-03-03 16:07:32 +0700 (Mon, 03 Mar 2008) $", "$Author:
 * qkp@ELCA.CH $" "$Id:$" );</script>
 * 
 * @author ltd
 */
public class SearchProjectsController extends SimpleFormController {
	private ProjectService m_projectService;
	
	/**
	 * Default constructor.
	 */
	public SearchProjectsController() {
		super();

		setSessionForm(true);
		setCommandName("query");
		setCommandClass(ProjectQuery.class);
		setFormView(UrlConstants.SEARCH_URL);
		setSuccessView(UrlConstants.REDIRECT_PREFIX + UrlConstants.SEARCH_URL);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		try {
			Object command = getCommand(request);
			ProjectQuery query = (ProjectQuery) command;
			if (query != null) {
				return query;
			}
			return super.formBackingObject(request);
		} catch (HttpSessionRequiredException e) {
			return super.formBackingObject(request);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ProjectQuery query = (ProjectQuery) command;
		List<Project> projects = getProjectService().getAllProjects();
		System.out.println(projects.size());
		request.getSession().setAttribute("projects", projects);

		return showForm(request, response, errors);
	}

	/**
	 * @param projectService
	 *            the projectService to set
	 */
	public void setProjectService(ProjectService projectService) {
		this.m_projectService = projectService;
	}

	/**
	 * @return the projectService
	 */
	public ProjectService getProjectService() {
		return m_projectService;
	}

}
